<!DOCTYPE html>
<html>
<head>
    <title>简化实体生成工具</title>
    <meta charset="utf-8">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
        }
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 3px solid #27ae60;
            padding-bottom: 15px;
        }
        .section {
            margin: 25px 0;
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            background-color: #fafafa;
        }
        .section h3 {
            color: #34495e;
            margin-top: 0;
            margin-bottom: 15px;
        }
        .button-group {
            margin: 15px 0;
        }
        button {
            background-color: #27ae60;
            color: white;
            border: none;
            padding: 12px 24px;
            margin: 5px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #229954;
        }
        button:disabled {
            background-color: #95a5a6;
            cursor: not-allowed;
        }
        button.test {
            background-color: #3498db;
        }
        button.test:hover {
            background-color: #2980b9;
        }
        .form-group {
            margin: 15px 0;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
            color: #2c3e50;
        }
        input, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 6px;
            white-space: pre-wrap;
            font-family: 'Courier New', monospace;
            font-size: 13px;
            max-height: 400px;
            overflow-y: auto;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .loading {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .highlight {
            background-color: #fff3cd;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 简化实体生成工具</h1>
        
        <div class="highlight">
            <strong>✨ 新功能特点：</strong>
            <ul>
                <li>使用 SQL 查询直接获取表结构，避免 SqlSugar DbFirst 的问题</li>
                <li>生成标准的 C# 实体类，语法正确，无重复字段</li>
                <li>自动处理数据类型转换和可空类型</li>
                <li>支持主键、自增、字段映射等 SqlSugar 特性</li>
                <li>逐个处理表，避免内存溢出问题</li>
            </ul>
        </div>
        
        <!-- 数据库连接测试 -->
        <div class="section">
            <h3>📡 数据库连接测试</h3>
            <div class="button-group">
                <button class="test" onclick="testConnection()">测试数据库连接</button>
            </div>
        </div>
        
        <!-- 生成常用表实体 -->
        <div class="section">
            <h3>⚡ 快速生成常用表实体</h3>
            <p>一键生成 integral_grade、borrower_attach 等常用表的实体类</p>
            <div class="button-group">
                <button onclick="generateCommonEntities()">生成常用表实体</button>
            </div>
        </div>
        
        <!-- 生成指定表实体 -->
        <div class="section">
            <h3>🎯 生成指定表实体</h3>
            <div class="form-group">
                <label for="tableNames">表名（多个用逗号分隔）:</label>
                <input type="text" id="tableNames" placeholder="例如: integral_grade,borrower_attach,dict" />
            </div>
            <div class="form-group">
                <label for="namespaceName">命名空间:</label>
                <input type="text" id="namespaceName" value="Model" />
            </div>
            <div class="button-group">
                <button onclick="generateEntitiesByNames()">生成指定表实体</button>
            </div>
        </div>
        
        <!-- 生成单个表实体 -->
        <div class="section">
            <h3>🔧 生成单个表实体</h3>
            <div class="form-group">
                <label for="singleTableName">表名:</label>
                <input type="text" id="singleTableName" placeholder="例如: integral_grade" />
            </div>
            <div class="button-group">
                <button onclick="generateSingleEntity()">生成单个表实体</button>
            </div>
        </div>
        
        <!-- 测试功能 -->
        <div class="section">
            <h3>🧪 测试功能</h3>
            <div class="button-group">
                <button class="test" onclick="testGenerateCommonTables()">测试生成常用表</button>
                <button class="test" onclick="testGenerateDictTable()">测试生成 dict 表</button>
                <button class="test" onclick="testGenerateSingleTable()">测试生成单个表</button>
            </div>
        </div>
        
        <!-- 结果显示 -->
        <div id="result"></div>
    </div>

    <script>
        const baseUrl = '/api/SimpleEntityGenerator';
        const testBaseUrl = '/api/SimpleEntityGeneratorTest';
        
        function showResult(message, type = 'info') {
            const resultDiv = document.getElementById('result');
            resultDiv.className = `result ${type}`;
            resultDiv.textContent = message;
        }
        
        function showLoading(message) {
            showResult(message + '...', 'loading');
        }
        
        async function testConnection() {
            showLoading('正在测试数据库连接');
            try {
                const response = await fetch(`${baseUrl}/TestConnection`);
                const data = await response.json();
                
                if (data.success) {
                    showResult(`✅ ${data.message}\n连接字符串: ${data.connectionString}\n测试结果: ${data.testResult}`, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}\n连接字符串: ${data.connectionString}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        async function generateCommonEntities() {
            showLoading('正在生成常用表实体');
            try {
                const response = await fetch(`${baseUrl}/GenerateCommonEntities`, {
                    method: 'POST'
                });
                const data = await response.json();
                
                if (data.success) {
                    let message = `✅ ${data.message}\n输出路径: ${data.outputPath}\n`;
                    if (data.tableNames && data.tableNames.length > 0) {
                        message += `成功生成: ${data.tableNames.join(', ')}\n`;
                    }
                    if (data.failedTables && data.failedTables.length > 0) {
                        message += `失败的表: ${data.failedTables.join(', ')}\n`;
                    }
                    if (data.generatedFiles && data.generatedFiles.length > 0) {
                        message += `生成文件: ${data.generatedFiles.join(', ')}`;
                    }
                    showResult(message, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        async function generateEntitiesByNames() {
            const tableNames = document.getElementById('tableNames').value.trim();
            const namespaceName = document.getElementById('namespaceName').value.trim() || 'Model';
            
            if (!tableNames) {
                showResult('❌ 请输入要生成实体的表名', 'error');
                return;
            }
            
            showLoading('正在生成指定表实体');
            try {
                const response = await fetch(`${baseUrl}/GenerateEntitiesByNames?tableNames=${encodeURIComponent(tableNames)}&namespaceName=${encodeURIComponent(namespaceName)}`);
                const data = await response.json();
                
                if (data.success) {
                    let message = `✅ ${data.message}\n输出路径: ${data.outputPath}\n`;
                    if (data.tableNames && data.tableNames.length > 0) {
                        message += `成功生成: ${data.tableNames.join(', ')}\n`;
                    }
                    if (data.failedTables && data.failedTables.length > 0) {
                        message += `失败的表: ${data.failedTables.join(', ')}\n`;
                    }
                    if (data.generatedFiles && data.generatedFiles.length > 0) {
                        message += `生成文件: ${data.generatedFiles.join(', ')}`;
                    }
                    showResult(message, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        async function generateSingleEntity() {
            const tableName = document.getElementById('singleTableName').value.trim();
            
            if (!tableName) {
                showResult('❌ 请输入要生成实体的表名', 'error');
                return;
            }
            
            showLoading('正在生成单个表实体');
            try {
                const response = await fetch(`${baseUrl}/GenerateSingleEntity?tableName=${encodeURIComponent(tableName)}`);
                const data = await response.json();
                
                if (data.success) {
                    let message = `✅ ${data.message}\n输出路径: ${data.outputPath}\n`;
                    if (data.tableNames && data.tableNames.length > 0) {
                        message += `成功生成: ${data.tableNames.join(', ')}\n`;
                    }
                    if (data.failedTables && data.failedTables.length > 0) {
                        message += `失败的表: ${data.failedTables.join(', ')}\n`;
                    }
                    if (data.generatedFiles && data.generatedFiles.length > 0) {
                        message += `生成文件: ${data.generatedFiles.join(', ')}`;
                    }
                    showResult(message, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        async function testGenerateCommonTables() {
            showLoading('正在测试生成常用表实体');
            try {
                const response = await fetch(`${testBaseUrl}/TestGenerateCommonTables`);
                const data = await response.json();
                
                if (data.success) {
                    showResult(`✅ ${data.message}\n详细信息:\n${JSON.stringify(data.details, null, 2)}`, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        async function testGenerateDictTable() {
            showLoading('正在测试生成 dict 表实体');
            try {
                const response = await fetch(`${testBaseUrl}/TestGenerateDictTable`);
                const data = await response.json();
                
                if (data.success) {
                    showResult(`✅ ${data.message}\n详细信息:\n${JSON.stringify(data.details, null, 2)}`, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        async function testGenerateSingleTable() {
            showLoading('正在测试生成单个表实体');
            try {
                const response = await fetch(`${testBaseUrl}/TestGenerateSingleTable`);
                const data = await response.json();
                
                if (data.success) {
                    showResult(`✅ ${data.message}\n详细信息:\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showResult(`❌ ${data.message}\n错误: ${data.error}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 请求失败: ${error.message}`, 'error');
            }
        }
        
        // 页面加载时自动测试连接
        window.onload = function() {
            testConnection();
        };
    </script>
</body>
</html>
